﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HA.ModFlow.Domain.DomainLayer;

using System.Collections.ObjectModel;
using HA.ModFlow.Infrastructure;

namespace HA.ModFlow.Controls.Windows
{
    /// <summary>
    /// winStressPeriod.xaml 的交互逻辑
    /// </summary>
    public partial class winStressPeriod : Window
    {
        public winStressPeriod(List<StressPeriod> StressPeriod, Units units)
        {
            InitializeComponent();

            //this.StressPeriod = new List<StressPeriod>();


            foreach (StressPeriod item in StressPeriod)
            {
                //this.StressPeriod.Add((StressPeriod)item.Clone());


                this._ocSPData.Add((StressPeriod)item.Clone());


            }

            this.DataContext = this._ocSPData;

            this.mGrid.ItemsSource = this._ocSPData;

            this.tbNSP.Text = this._ocSPData.Count.ToString();


            if (this._ocSPData.Count <= 1)
            {
                this.btnDel.IsEnabled = false;

            }

            this.labelUnit.Content = "(" + Domain.DomainLayer.UnitConverter.GetTime(units.Time) + ")";



            MenuItem itemCopy = new MenuItem();
            itemCopy.Header = "复制";
            itemCopy.Command = ApplicationCommands.Copy;

            //MenuItem itemPaste2 = new MenuItem();
            //itemPaste2.Header = "粘贴";
            //itemPaste2.InputGestureText = "Ctrl+V";
            //itemPaste2.Click += new RoutedEventHandler(itemPaste2_Click);

            ContextMenu cm = new ContextMenu();
            cm.Items.Add(itemCopy);
            //cm.Items.Add(itemPaste2);          
            this.mGrid.ContextMenu = cm;

        }

        void itemPaste2_Click(object sender, RoutedEventArgs e)
        {
            //取得剪贴板数据


            List<string[]> rowData = HAUtilWpf.ClipboardHelper.ParseClipboardData();

            if (rowData == null || rowData.Count == 0)
            {
                return;


            }

            //取得选中单元的行序号

            int minRowIndex = HAUtilWpf.DataGridHelper.GetRowIndex(this.mGrid.SelectedCells[0]);

            //下面的代码写的太过蹊跷，应该取得的是选中的行号吧?


            int maxRowIndex = this.mGrid.Items.Count - 1;
            int minColumnDisplayIndex = this.mGrid.SelectedCells[0].Column.DisplayIndex;
            int maxColumnDisplayIndex = this.mGrid.Columns.Count - 1;

            int rowDataIndex = 0;
            for (int i = minRowIndex; i <= maxRowIndex && rowDataIndex < rowData.Count; i++, rowDataIndex++)
            {
                int columnDataIndex = 0;
                for (int j = minColumnDisplayIndex; j <= maxColumnDisplayIndex && columnDataIndex < rowData[rowDataIndex].Length; j++, columnDataIndex++)
                {
                    if (rowData[rowDataIndex][columnDataIndex] != "")
                    {
                       
                        double value = 0;
                        bool b = double.TryParse(rowData[rowDataIndex][columnDataIndex], out value);
                        if (b)
                        {
                            //this._pcDs.CurrentLayerDouble[i, j] = value;
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
            }
            this.mGrid.Items.Refresh();
        }

     

        public List<StressPeriod> StressPeriod
        {
            get { return this._ocSPData.ToList(); }
            
        }


        ObservableCollection<StressPeriod> _ocSPData=new ObservableCollection<StressPeriod> ();


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            this.DataContext = this.StressPeriod;
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {        
            this.DialogResult = true;

            this.Close();
           
        }

        private void btnCancal_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnDel_Click(object sender, RoutedEventArgs e)
        {
            //int a = this.mGrid.SelectedIndex;
            //if (a >= 0 && this.mGrid.Items.Count > 0)
            //{
            //    this.StressPeriod.RemoveAt(a);
            //    this.DataContext = this.StressPeriod;
            //    this.mGrid.Items.Refresh();
            //    if (a >= this.mGrid.Items.Count)
            //    {
            //        a = this.mGrid.Items.Count - 1;
            //    }
            //    this.mGrid.SelectedIndex = a;
            //}


            int index = this.mGrid.SelectedIndex;

            if (index>=0)
            {
                this._ocSPData.RemoveAt(index);

                if (this._ocSPData.Count<=1)
                {
                    this.btnDel.IsEnabled = false;
                    
                }


                this.tbNSP.Text = this._ocSPData.Count.ToString ();

                this.mGrid.SelectedIndex = index-1;

                this.mGrid.Items.Refresh();


                
            }




           
        }

        private void btnInsert_Click(object sender, RoutedEventArgs e)
        {
            //int a = this.mGrid.SelectedIndex;

            //if (a < 0)
            //{
            //    a = 0;
            //}
            //this.StressPeriod.Insert(a, new StressPeriod());
            //this.DataContext = this.StressPeriod;
            //this.mGrid.Items.Refresh();
            //this.mGrid.SelectedIndex = a;

            this._ocSPData.Add(new StressPeriod());

            if (this._ocSPData.Count > 1)
            {
                this.btnDel.IsEnabled = true;

            }


            this.tbNSP.Text = this._ocSPData.Count.ToString();


        }

        private void mGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }
    }


    internal class TotalTimeConvertor : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            double length=0;

            IList<StressPeriod> lst = value as IList<StressPeriod>;
            

            //ObservableCollection<StressPeriod> lst = value as ObservableCollection<StressPeriod>;
            if (lst!=null)
            {
                foreach (StressPeriod item in lst)
                {
                    length += item.PERLEN;


                }
                
            }
           

            return length;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


    internal class IsEnabledConvertor : IValueConverter
    {


        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            IList<StressPeriod> lst = value as IList<StressPeriod>;

            if (lst==null)
            {
                return false;

                
            }

            if (lst.Count > 1)
            {
                return true;

            }
            else
            {
                return false;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

}
